#include <sys/time.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

long app_common_getCurrentTime(void)
{
    struct timeval tv;
    gettimeofday(&tv, NULL);
    /*sec：秒   usec：微秒*/
    return (tv.tv_sec * 1000 + tv.tv_usec / 1000);
}


/**
 * @brief 字符数组转16进制字符串 abcd=> 61 62 63 64
 */
char *app_common_chars2Hexstr(char *chars, int chars_len)
{
    //先申请16进制字符串的内存空间
    char *hexstr = (char *)malloc(chars_len * 2 + 1);
    for(int i=0;i<chars_len;i++)
    {
        sprintf(hexstr + i * 2, "%02x", chars[i]);
    }
    hexstr[chars_len * 2] = '\0';
    //将字符数组转成16进制字符串
    return hexstr;
}

/**
 * @brief 16进制字符串转字符数组 61 62 63 64=>abcd
 */
char *app_common_hexstr2Chars(char *hexstr, int *chars_len)
{
    //得到16进制字符串的长度
    int hexstr_len = strlen(hexstr);
    //转为字符数组需要一半的长度
    *chars_len = hexstr_len / 2;
    //先申请字符数组的内存空间
    char *chars = (char *)malloc(*chars_len + 1);
    for(int i=0;i<(hexstr_len);i+=2)
    {
        sscanf(hexstr + i,"%2x",(unsigned int *)(chars + i / 2));
    }
    //返回字符数组
    return chars;

}

